﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Web企业级开发II.Service;
using Web企业级开发II.Models.Database;
using Web企业级开发II.Models;

namespace Web企业级开发II.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthenticationController : ControllerBase
    {
        //注入jwt服务
        private readonly IJWTService _jwtService;
        //定义数据库上下文
        private readonly Store_2022Context _db;
        /// <summary>
        /// 构造注入
        /// </summary>
        /// <param name="jwtService"></param>
        public AuthenticationController(IJWTService jwtService, Store_2022Context db)
        {
            _jwtService = jwtService;
            _db = db;
        }

        /// <summary>
        /// 创建token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string CreateToken(string name)
        {
            //返回  通过用户名 创建token
            return _jwtService.CreateToken(name, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        //标签 接口需要授权
        [Authorize]
        public string UserInfo()
        {
            //获取用户信息           登录用户 消息载荷 查询第一条（条件是Type==）   获取登录用户的用户名
            return Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
        }

        /// <summary>
        /// 注册 添加新用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string AddUser(AddUserRequest request)
        {
            //唯一的用户名
            //当数据库存在这个用户名时 做出响应
            //存在
            if (_db.Users.Any(x => x.UserName == request.UserName))
            {
                return "用户名已存在";
            }
            //获取到请求参数了

            //使用参数
            //添加新用户

            var user = new User
            {
                UserName = request.UserName,
                Password = request.Password,
                CraeteTime = DateTime.Now,
                Photo = string.Empty,
                Desc = string.Empty,
            };
            _db.Users.Add(user);
            var row = _db.SaveChanges();
            if (row > 0)
                return "注册成功";
            return "注册失败";
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string LoginAccount(LoginAccountRequest request)
        {
            //登录 逻辑

            //校验用户

            //1、先把用户查询出来

            var user = _db.Users.FirstOrDefault(x => x.UserName == request.UserName);
            if (user == null)
                return "用户不存在";

            //2、校验密码
            if (user.Password != request.PassWord)
                return "密码不正确";

            //都正确 确认是这个人
            return _jwtService.CreateToken(user.UserName, user.UserId);
        }

    }
}
